<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Ldap
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: AttributeIterator.php 10 2008-11-08 17:17:02Z Stefan.Gehrig.HN $
 */

/** 
 * Zend_Ldap_Node 
 */
require_once 'Zend/Ldap/Node.php';


/**
 * Zend_Ldap_Node_AttributeIterator provides an iterator to the attribute collection.
 *
 * @category   Zend
 * @package    Zend_Ldap
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Ldap_Node_AttributeIterator implements Iterator 
{
    /**
     * The underlying node.
     *
     * @var Zend_Ldap_Node
     */
    private $_node;
    
    /**
     * The current iteration index.
     *
     * @var integer
     */
    private $_index;
    
    /**
     * All attribute keys.
     *
     * @var array
     */
    private $_keys;
    
    public function __construct(Zend_Ldap_Node $node)
    {
        $this->_node=$node;
        $this->rewind();
    }
    
    /**
     * Return the current attribute.
     * Implements Iterator
     *
     * @return array
     */
    public function current()
    {
        return $this->_node->getAttribute($this->key(), null);
    }
    
    /**
     * Return the attribute name.
     * Implements Iterator
     *
     * @return string
     */
    public function key()
    {
        return $this->_keys[$this->_index];
    }
    
    /**
     * Move forward to next attribute.
     * Implements Iterator
     */
    public function next()
    {
        $this->_index++;
    }
    
    /**
     * Rewind the Iterator to the first attribute.
     * Implements Iterator
     */
    public function rewind()
    {
        $this->_index=0;
        $this->_keys=$this->_node->getAttributeKeys();
        sort($this->_keys, SORT_STRING);
    }
    
    /**
     * Check if there is a current attribute
     * after calls to rewind() or next().
     * Implements Iterator
     *
     * @return bool
     */
    public function valid()
    {
        return ($this->_index>=0 && $this->_index<count($this->_keys)); 
    }
}